package org.sxp.modules.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.sxp.common.annotation.SysLog;
import org.sxp.common.utils.*;
import org.sxp.common.web.WrapperResponse;
import org.sxp.modules.sys.entity.SysDeptEntity;
import org.sxp.modules.sys.service.SysDeptService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 部门管理
 *
 *
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	public static final String HTTP = "http";
	public static final String PREFIX = "/";
	@Autowired
	private SysDeptService sysDeptService;

	@Value("${deptSyn.medinssetUrl:/ims/web/his/api/scen/public/medinssetList}")
	private String medinssetUrl;

	@Value("${deptSyn.admdvsUrl:/ims/web/his/api/scen/public/admdvsinfoList}")
	private String admdvsListUrl;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public List<SysDeptEntity> list(String parentId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId", parentId);

		List<SysDeptEntity> deptList = sysDeptService.queryList(params);

		return deptList;
	}
	/**
	 * 列表-分页
	 */
	@RequestMapping("/listPage")
	public AeyePageResult listPage(String parentId, String name) throws Exception{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId", parentId);
		params.put("name", name);

		PageInfo pageInfo = buildPageInfo();

		AeyePageResult deptList = sysDeptService.queryListPage(params, pageInfo);

		return deptList;
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	public R select(){
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());

		//添加一级部门
		if(isAdmin()){
			SysDeptEntity root = new SysDeptEntity();
			root.setDeptId("0");
			root.setName("一级部门");
			root.setParentId("-1");
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	public R info(){
		Integer deptId = 0;
		if(!isAdmin()){
			List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());
			Integer parentId = null;
			for(SysDeptEntity sysDeptEntity : deptList){
				if(parentId == null){
					parentId = Integer.parseInt(sysDeptEntity.getParentId());
					continue;
				}

				if(parentId > Integer.parseInt(sysDeptEntity.getParentId())){
					parentId = Integer.parseInt(sysDeptEntity.getParentId());
				}
			}
			deptId = parentId;
		}

		return R.ok().put("deptId", deptId);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	public R info(@PathVariable("deptId") String deptId){
		SysDeptEntity dept = sysDeptService.getById(deptId);
		
		return R.ok().put("dept", dept);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@SysLog("保存部门")
	public R save(@RequestBody SysDeptEntity dept){
		dept.setDeptId(AeyeIdUtils.generateShortUuid());
		sysDeptService.save(dept);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@SysLog("修改部门")
	public R update(@RequestBody SysDeptEntity dept){
		sysDeptService.updateById(dept);
		
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@SysLog("删除部门")
	public WrapperResponse delete(String deptId)throws Exception{
		//判断是否有子部门
		List<String> deptList = sysDeptService.queryDetpIdList(deptId);
		if(deptList.size() > 0){
			return WrapperResponse.fail("请先删除子部门", null);
		}

		sysDeptService.removeById(deptId);

		return WrapperResponse.success(null);
	}
	/**
	 * 强制删除
	 */
	@RequestMapping("/deleteAll")
	@SysLog("强制删除部门")
	public WrapperResponse deleteAll(String deptId)throws Exception{
		//判断是否有子部门
		List<String> deptList = sysDeptService.getSubDeptIdList(deptId);
		for ( String item :  deptList){
			sysDeptService.removeById(item);
		}

		sysDeptService.removeById(deptId);

		return WrapperResponse.success(null);
	}



	public String getParentId(String deptId){
		if(deptId.endsWith("0000")){
			return "1";
		}else if(deptId.endsWith("00")){
			return deptId.substring(0, 2)+"0000";
		}else {
			return deptId.substring(0, 4)+"00";
		}
	}
}
